﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Props.Backend;

namespace Props.Tests.Backend
{
    [TestClass]
    public class AddTestsUsers
    {
        [TestMethod]
        public void AddTestUsers()
        {
            AddTestUser1();
            AddTestUser2();
        }

        private static void AddTestUser1()
        {
            var email = "juan.fernandez@propiedades.com";
            var firstName = "Juan";
            var lastName = "Fernandez";
            var pictureUrl = @"http://www.american.edu/uploads/profiles/large/chris_palmer_profile_11.jpg";

            AddTestUser(email, firstName, lastName, pictureUrl);
        }

        private static void AddTestUser2()
        {
            var email = "maria.martinez@propiedades.com";
            var firstName = "Maria";
            var lastName = "Martinez";
            var pictureUrl = @"http://s3.torbit.com/img/a916cb9a306c6f685eb9b572d81965d2083e0ad9-PIC-LaurenKennedy.jpg";

            AddTestUser(email, firstName, lastName, pictureUrl);
        }

        private static void AddTestUser(string email, string firstName, string lastName, string pictureUrl)
        {
            var users = RepositoryFactory.Users;
            var userProfiles = RepositoryFactory.UserProfiles;

            var user = users.Get(u => u.Email == email).FirstOrDefault();

            if (user == null)
            {
                user = new User
                {
                    Email = email,
                    Password = "123456"
                };

                users.Save(user);
            }

            var userProfile = userProfiles.Get(u => u.Id == user.Id).FirstOrDefault();

            if (userProfile == null)
            {
                userProfile = new UserProfile
                {
                    Id = user.Id,
                    Email = email,
                    FirstName = firstName,
                    LastName = lastName,
                    PictureUrl = pictureUrl
                };

                userProfiles.Save(userProfile);
            }
        }
    }
}
